0%

react native中一些实用的方法

一些在 react native 中实用的方法

Text 中实现换行

要实现在 Text 组件中进行换行,需要在文本中间加入 {'\n'}

1
2
3
4
5
<Text>
11111111111
{"\n"}
22222222222
</Text>

测量控件的宽高

使用 view 的 onLayout 方法

1
2
3
4
5
<Text onLayout={(e) => this._layout(e)}></Tex>

...

_layout = e => console.log(e)

使用标签的 measure 方法

1
2
3
4
5
6
7
8
9
10
11
<Text ref='text'></Text>

...

componentDidMount () {
setTimeout(() => {
this.refs.text.measure((x, y, width, height, left,top) => {
console.log(x, y, width, height, left, top)
})
})
}

如果不使用 setTimeout 会出现结果为 0 的情况